package com.itcast.netty.server;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zheng.zhang
 */
@Slf4j
public class DiscardServerHandler extends SimpleChannelInboundHandler<ByteBuf> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) {
        log.info("传输的内容是:{}", msg.toString(CharsetUtil.UTF_8));
        ByteBuf response = Unpooled.copiedBuffer("收到信息:$".getBytes());
        ctx.writeAndFlush(response);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        // 出现异常就关闭
        cause.printStackTrace();
        ctx.close();
    }
}
